home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / ltp_printboxline.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  4KB  |  237 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #include "Assert.h"
  15.  
  16.  
  17. /*****************************************************************************/
  18.  
  19.  
  20. BOOL
  21. LTP_PrintLinePadded(LayoutHandle *Handle,LONG Left,LONG Top,LONG Space,STRPTR Line,LONG Len,LONG textPen,LONG backPen)
  22. {
  23.     LONG Size,Start,Count,i,Width;
  24.     struct RastPort *RPort;
  25.  
  26.     RPort    = &Handle->RPort;
  27.     Size    = 0;
  28.     Start    = 0;
  29.     Count    = 0;
  30.     i        = 0;
  31.     Width    = 0;
  32.  
  33.     while(i < Len && Line[i] == ' ')
  34.     {
  35.         Size++;
  36.         i++;
  37.     }
  38.  
  39.     while(i < Len)
  40.     {
  41.         while(i < Len && Line[i] != ' ')
  42.         {
  43.             Size++;
  44.             i++;
  45.         }
  46.  
  47.         Width += TextLength(RPort,&Line[Start],Size);
  48.  
  49.         Count++;
  50.  
  51.         while(i < Len && Line[i] == ' ')
  52.             i++;
  53.  
  54.         Start    = i;
  55.         Size    = 0;
  56.     }
  57.  
  58.     if(Width)
  59.     {
  60.         LONG j,TextLeft;
  61.  
  62.         Space -= Width;
  63.  
  64.         j = Start = Size = 0;
  65.  
  66.         while(j < Len && Line[j] == ' ')
  67.         {
  68.             Size++;
  69.             j++;
  70.         }
  71.  
  72.         LTP_SetAPen(RPort,textPen);
  73.  
  74.         TextLeft = Left;
  75.  
  76.         Count--;
  77.  
  78.         for(i = 0 ; i <= Count ; i++)
  79.         {
  80.             if(i)
  81.             {
  82.                 LONG Delta;
  83.  
  84.                 if(Delta = (Space * i) / Count - (Space * (i - 1)) / Count)
  85.                 {
  86.                     LTP_SetAPen(RPort,backPen);
  87.                     LTP_FillBox(RPort,TextLeft,Top,Delta,Handle->GlyphHeight);
  88.                     LTP_SetAPen(RPort,textPen);
  89.  
  90.                     TextLeft += Delta;
  91.                 }
  92.             }
  93.  
  94.             while(j < Len && Line[j] != ' ')
  95.             {
  96.                 Size++;
  97.                 j++;
  98.             }
  99.  
  100.             LTP_PrintText(RPort,&Line[Start],Size,TextLeft,Top);
  101.  
  102.             TextLeft += TextLength(RPort,&Line[Start],Size);
  103.  
  104.             while(j < Len && Line[j] == ' ')
  105.                 j++;
  106.  
  107.             Start = j;
  108.             Size = 0;
  109.         }
  110.  
  111.         return(TRUE);
  112.     }
  113.     else
  114.         return(FALSE);
  115. }
  116.  
  117.  
  118. /*****************************************************************************/
  119.  
  120.  
  121. VOID
  122. LTP_PrintLine(LayoutHandle *handle,LONG alignType,LONG left,LONG top,LONG space,STRPTR line,LONG len,LONG textPen,LONG backPen)
  123. {
  124.     struct RastPort *rp;
  125.  
  126.     /* New in 40.11: auto-adapt pen numbers if -1. */
  127.  
  128.     if(textPen == -1)
  129.         textPen = handle->TextPen;
  130.  
  131.     if(backPen == -1)
  132.         backPen = handle->BackgroundPen;
  133.  
  134.     rp = &handle->RPort;
  135.  
  136.     LTP_SetPens(rp,backPen,backPen,JAM2);
  137.  
  138.     if(alignType == ALIGNTEXT_PAD)
  139.     {
  140.         if(LTP_PrintLinePadded(handle,left,top,space,line,len,textPen,backPen))
  141.             return;
  142.     }
  143.     else
  144.     {
  145.         struct TextExtent extent;
  146.         LONG width;
  147.  
  148.         len        = TextFit(rp,line,len,&extent,NULL,1,space,32767);
  149.         width    = extent.te_Width;
  150.  
  151.         if(len)
  152.         {
  153.             LONG textLeft;
  154.  
  155.             switch(alignType)
  156.             {
  157.                 case ALIGNTEXT_LEFT:
  158.  
  159.                     textLeft = left;
  160.  
  161.                     if(width < space)
  162.                         LTP_FillBox(rp,left + width,top,space - width,handle->GlyphHeight);
  163.  
  164.                     break;
  165.  
  166.                 case ALIGNTEXT_CENTERED:
  167.  
  168.                     textLeft = left + (space - width) / 2;
  169.  
  170.                     if(width < space)
  171.                     {
  172.                         LONG fill;
  173.  
  174.                         if((fill = (space - width) / 2) > 0)
  175.                             LTP_FillBox(rp,left,top,fill,handle->GlyphHeight);
  176.                         else
  177.                             fill = 0;
  178.  
  179.                         fill += width;
  180.  
  181.                         if(fill < space)
  182.                             LTP_FillBox(rp,left + fill,top,space - fill,handle->GlyphHeight);
  183.                     }
  184.  
  185.                     break;
  186.  
  187.                 case ALIGNTEXT_RIGHT:
  188.  
  189.                     textLeft = left + space - width;
  190.  
  191.                     if(width < space)
  192.                         LTP_FillBox(rp,left,top,space - width,handle->GlyphHeight);
  193.  
  194.                     break;
  195.  
  196.                 default:
  197.  
  198.                     return;    /* Note: only in case none of the alignment types match; should never happen! */
  199.             }
  200.  
  201.             LTP_SetAPen(rp,textPen);
  202.             LTP_PrintText(rp,line,len,textLeft,top);
  203.  
  204.             return;
  205.         }
  206.     }
  207.  
  208.     LTP_FillBox(rp,left,top,space,handle->GlyphHeight);
  209. }
  210.  
  211.  
  212. /*****************************************************************************/
  213.  
  214.  
  215. VOID
  216. LTP_PrintBoxLine(LayoutHandle *handle,ObjectNode *node,LONG index)
  217. {
  218.     if(node->Special.Box.Lines && node->Special.Box.Lines[index])
  219.     {
  220.         LTP_PrintLine(handle,node->Special.Box.AlignText,node->Left + 4,node->Top + 2 + index * (handle->GlyphHeight + node->Special.Box.Spacing),node->Width - 8,node->Special.Box.Lines[index],strlen(node->Special.Box.Lines[index]),node->Special.Box.TextPen,node->Special.Box.BackPen);
  221.  
  222.         if(index < node->Lines - 1 && node->Special.Box.Spacing > 0)
  223.         {
  224.             struct RastPort *rp = &handle->RPort;
  225.             LONG pen;
  226.  
  227.             if(node->Special.Box.BackPen == -1)
  228.                 pen = handle->BackgroundPen;
  229.             else
  230.                 pen = node->Special.Box.BackPen;
  231.  
  232.             LTP_SetPens(rp,pen,pen,JAM2);
  233.             LTP_FillBox(rp,node->Left + 4,node->Top + 2 + (index + 1) * (handle->GlyphHeight + node->Special.Box.Spacing),node->Width - 8,node->Special.Box.Spacing);
  234.         }
  235.     }
  236. }
  237.